<?php
import("@.ORG.Page");
//相册应用 - UploadAction 上传图片 及 处理
class UploadAction extends Action{
   /**
     * @author sudi
     * @date 2012.1.7
     * 添加视频主界面
     */
    public function _initialize() {
        $pid=Session::get('p_id');

	}     
    
    /**
     * @author sudi
     * @date 2012.1.7
     */
     function uploadpicture(){
        $Dao = M('Photoalbum');
        $condition['uid'] = Session::get('uid');
        $List = $Dao->where($condition)->findAll();
        $this->assign('list',$List);
        $this->display("Tpl/default/Upload/addphoto");
     } 
  	/**
     * @author sudi
     * @date 2012.1.7
     * 多数文件上传 
     */
    public function photofile()
    {
       echo $_REQUEST['PHPSESSID'];
       if(isset($_REQUEST['PHPSESSID'])) {
           session_id($_REQUEST['PHPSESSID']);    // 调用 session_id function 放入 session id
           session_start();
       }
       
         if (!empty($_FILES)) {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
            $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
            move_uploaded_file($tempFile,$targetFile);
            echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
        }   
        
       $photos=$_FILES['Filedata']['tmp_name'];;
      echo $photos;
      $this->show_photo();
    } 
   	/**
     * @author sudi
     * @date 2012.4.28
     * 删除图片
     */ 
    public function deletePicture(){
        $picId = $_REQUEST['picid'];
        
        if($picId == 0){
            echo "<script>alert('删除失败，请重新尝试!'); 
             window.history.back();</script>";
        }
        $condtion['id']=$picId;
        $List = M('Photos')->where($condtion)->find();
        $uid = $List['uid'];
        $condcomm['pho_id'] = $picId;
        $Last = M('Photos')->where($condtion)->delete();
        $LastComm = M('Photo_comment')->where($condcomm)->delete();
        $this->albumList($uid);   
    } 
	/**
     * @author sudi
     * @date 2012.1.5
     * 传递参数 
     */
    public function add_photo()
    {
       $photo=$_FILES['photo'];
       $phtag =$_REQUEST['photo_tag']; 
       $describe =$_REQUEST['ph_describe'];
       $albumid = $_REQUEST['album'];
       $this->upload_image($photo,$phtag,$describe,$albumid);
    }    
 /**
  * @author sudi
  * @date 2012.1.5
  * 普通上传图片
  * 
  */
    function upload_image($imgpath,$ph_tag,$describe,$albumid)
     {
       $name=time()."_".$imgpath['name'];
       $tmp_name=$imgpath["tmp_name"];
       $type=$imgpath["type"];
       $size=$imgpath["size"];
       $err = $imgpath["error"];
       $path = "/home/Tpl/default/Public/upload/".$name;
       $uploadfile = SITE_PATH.$path;
       $maxsize=800*1024;          //最大允许上许文件大小
       if($name=="")                                             //文件名为空
         {
         echo"<script>alert('请先选择要上传的图片文件!'); 
         window.history.back();</script>";
         }
        if($albumid=="")                                             //文件名为空
         {
         echo"<script>alert('请先选择图片相册!'); 
         window.history.back();</script>";
         }
       if($type!="image/pjpeg" && $type!="image/jpeg" && $type!="image/gif")//文件类型不在指定范围
        {
         echo"<script>alert('上传文件只可以是JPEG或GIF类型的!');
         window.history.back();</script>";
         exit;
        }
       if($size>$maxsize)                                       //超过规定大小
       {
         echo"<script>alert('上传文件大小不能超过800K! ');window.history.back();</script>";
         exit;
       }
     
        if($err>0){
            switch($err) 
            { 
            case 1: echo "<script>alert('文件大小超过服务器限制! ');window.history.back();</script>"; 
            break; 
            case 2: echo "<script>alert('上传文件太大小! ');window.history.back();</script>"; 
            break; 
            case 3: echo "<script>alert('文件至加载一半，请重新加载! ');window.history.back();</script>"; 
            break; 
            case 4: echo "<script>alert('文件加载失败! ');window.history.back();</script>"; 
            break; 
            } 
          }  
         $photoDao = new PhotoModel;
         $condalbum['id'] = $albumid;
         $albumDao = M('Photoalbum');
         $albumpicture = $albumDao->where($condalbum)->find();
        
         if($albumpicture['picture']==null)
         {
            $data['picture'] = $name;
            $albumDao->where($condalbum)->save($data);
         }
        $lastid = $photoDao->insert_photo(Session::get('uid'),$albumid,$name,$path,time(),$ph_tag,$describe);
       
        if($lastid>0){
          if(move_uploaded_file($tmp_name,$uploadfile))
            {
            $this->showAlbum();
            }
          else if (copy($tmp_name,$uploadfile))
            {
            $this->showAlbum();
            }
          else
            return false; 
        }     
        else
            return false;    
  }
  
  /**
   * 添加相册
   * @author sudi
   */
   function addAlbum()
   {
      $name = $_REQUEST['albumname'];
      if(strlen($name)>46)
      {
        echo "<script>alert('输入字符传过长，15字以内！');window.history.back();</script>";
      }
      $Dao = D('photoalbum');
      $data['albumname'] = $name;
      $data['ctime'] = time();
      if(Session::get('uid')==null)
      {
        echo "<script>alert('用户你没有登录');window.history.back();</script>";
      }
      $data['uid'] = Session::get('uid');
      $Last = $Dao->add($data);
      $this->uploadpicture();
   }
  
   /**
    * @author sudi
    * 调用相册列表接口
    */
   function showAlbum()
   {
      $uid = Session::get('uid');
      $this->albumList($uid);
   }
   
   /**
    * @author sudi
    * 相册列表显示
    * 
    */
    function albumList($uid)
    {  
        $Form=M('Photoalbum');
        $condition['uid'] = $uid;
       
        $count=$Form->where($condition)->count();  
        $p = new Page ($count,12);
                 //  获得blog的列表
       $albumList=$Form->where($condition)->limit($p->firstRow.','.$p->listRows)->findAll();   
       $p->setConfig('header','条记录');
       $p->setConfig('prev',"<");
       $p->setConfig('next','>');
   	   	$p->setConfig('first','<<');
       $p->setConfig('last','>>');
       $page = $p->show();
        foreach($albumList AS $key=>$val){                
          $albumList[$key]['ctime']=date('Y-m-d H:i:s',$val['ctime']);                   
        }             
        $this->assign('page', $page);
  		$this->assign ( "list", $albumList );
        $this->display('Tpl/default/Upload/albumlist.html');     
    }
   
   /**
    * 调用显示相册列表
    * @author sudi
    */
    function showphoto()
    {
        $uid = Session::get('uid');
        $albumid = $_REQUEST['albumid'];
        if($uid==null)
        {
          echo "<script>alert('用户你没有登录');window.history.back();</script>";
        }
        $this->photoList($uid,$albumid);
    }
    
   /**
    * 从公共调用显示相册列表
    * @author sudi
    */
    function getPublicChild($uid,$albumid)
    {
        if($uid==null)
        {
          echo "<script>alert('用户你没有登录');window.history.back();</script>";
        }
        $this->photoList($uid,$albumid);
    }
    
   /**
    * @author sudi
    * 根据uid和相册的id获得该相册里图片显示
    * 
    */
    function photoList($uid,$albumid)
    {  
        $Form=M('Photos');
		import("@.ORG.Page"); //导入分页类
        $condition['uid'] = $uid;
        $condition['albumid'] = $albumid;
		$count = $Form->where($condition)->count();//    计算总数
		$p = new Page ($count, 12);
		$list=$Form->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
        $page = $p->show();
		$this->assign ( "page", $page );
		$this->assign ( "list", $list );
        $this->display('Tpl/default/Upload/photoinfo.html');        
    }
       
/**
 * @time:2012年3月3日15:21:06
 * 显示单张图片
 */
    public function fullShow()
    {
        $picID = $_REQUEST['picid'];
        $this->oneShow($picID);
       
    }
    
    public function oneShow($picID)
    {
        $Form=M('Photos');
        $condition['id'] = $picID;
        $list=$Form->where($condition)->find();

        //查找评论列表
        $cond['pho_id'] = $picID;
        $count=D('photo_comment')->where($cond)->count();  
        $p = new Page ($count,3);
         //  获得评论的列表
        $CommentList=D('photo_comment')->where($cond)->limit($p->firstRow.','.$p->listRows)->findAll();
        $p->setConfig('header','条记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');
        $page = $p->show();
          
        foreach($CommentList AS $key=>$val){
          $CommentList[$key]['ptime']=date('Y-m-d H:i',$val['ptime']); 
        }
        $this->assign("CommentList",$CommentList);
        $this->assign ( "list", $list );
        $this->assign ( "page", $page );
        $userid = Session::get('uid');
        if($list['uid'] == $userid){
             $this->display('Tpl/default/Upload/photo.html');  
        }else{
            $this->display('Tpl/default/Upload/publicphoto.html');  
        }
    }

 /**
  * 添加评论
  * @author:sudi
  */
    public function addPhotoComment()
    {
        $content = $_POST['phcomment']; 
        $photoId = $_POST['photoid'];
        $Comm = M('photo_comment');
        $data['content']= $content;
        $data['p_name'] = Session::get('nick');
        $data['pho_id']= $photoId;
        $data['ptime']= time();
        $last = $Comm->add($data); 
        $condtion['id'] = $last;
        $List= $Comm->where($condtion)->find();
        $content = $List['content']; 
        $pTime = date("Y-m-d H:i:s",$List['ptime']);
        $this->ajaxReturn($List,$pTime,1,'json');  
    }
    
    
    public function doPage(){ 
        //echo $_GET["p"]; 
        if(emptyempty($_GET["p"])) 
            $this->assign('p', 1); 
        else  
            $this->assign('p', $_GET["p"]); 
        $Nav = M("Photo"); 
        $count = $Nav->count(); 
        import("ORG.Util.Page"); 
        $p = new Page($count, 10); 
        $page = $p->show(); 
        $list = $Nav->order('id desc')->limit($p->firstRow.','.$p->listRows)->select(); 
         
        $this->assign('page', $page); 
        $this->assign("list",$list); 
    } 
    /**
     * @author sudi
     * 展示相册列表
     */
    
     function photoalbum()
    {  
        $Form=M('Photoalbum');
        $condition['uid'] = Session::get('uid');
        $List = $Form->where($condition)->findAll();
        $this->assign ( "list", $list );
        $this->display('Tpl/default/Upload/photoinfo.html');
    }
  

}
?>